#version 450
#extension GL_ARB_separate_shader_objects : enable

layout(constant_id = 0)
const uint numBaseTextures = 1;

layout(set = 0, binding = 0)
uniform sampler2D baseTextureSampler[numBaseTextures];

layout(set = 1, binding = 0) uniform TileSettings 
{
ivec3 tileIndex;
}
tileSettings;

layout(set = 2, binding = 0) uniform MyColor
{
    float colorID;
    float alpha;
    float lightSrength;
}
mycolor;

layout(location = 0) in
vec4 inUV;
layout (location = 1) in
float inLodBias;
layout (location = 2) in
vec3 inNormal;
layout (location = 3) in
vec3 inViewVec;
layout (location = 4) in
vec3 inLightVec;
layout (location = 5) in
vec3 inIJK;


layout(location = 0) out
vec4 outColor;

void main()
{
    float texcoords[4] = { inUV.x, inUV.y, inUV.z, inUV.w };
    int showID = int(mycolor.colorID + 0.5);
    float sampleTexcoord = texcoords[showID];
    vec4 color = texture(baseTextureSampler[0], vec2(sampleTexcoord, 0.5));
    vec3 N = normalize(inNormal);
	vec3 LightVec = vec3(1,1,1);
    vec3 L = normalize(LightVec);
    vec3 V = normalize(inViewVec);
    vec3 R = reflect(-L, N);
    vec3 diffuse = max(dot(N, L), 0.0) * mycolor.lightSrength * vec3(3.0);
    float specular = pow(max(dot(R, V), 0.0), 32.0) * color.a;
    vec4 ambient = 0.3 * color;
    ambient.a = 1.0;
    outColor = ambient + vec4(diffuse * color.rgb + specular, 1.0);
    //outColor = vec4(diffuse * color.rgb, 1.0);
    int ix = int(inIJK.x + 0.5);
    int iy = int(inIJK.y + 0.5);
    int iz = int(inIJK.z + 0.5);  
    if (ix == tileSettings.tileIndex.x && iy == tileSettings.tileIndex.y && iz == tileSettings.tileIndex.z)
    {
        outColor = 1 - outColor;
    }
    //outColor = vec4(inNormal.xyz, 1.0);
    outColor.a = mycolor.alpha;  
    outColor.r = pow(outColor.r, 1 / 2.2);
    outColor.g = pow(outColor.g, 1 / 2.2);
    outColor.b = pow(outColor.b, 1 / 2.2);
}
